package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.lock.annotation.Lock4j;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.encrypt.annotation.ApiEncrypt;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysMiniAdminBo;
import org.dromara.system.domain.bo.SysMiniBannerBo;
import org.dromara.system.domain.vo.SysMiniAdminVo;
import org.dromara.system.domain.vo.SysMiniBannerVo;
import org.dromara.system.service.ISysMiniAdminService;
import org.dromara.system.service.ISysMiniBannerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序管理员账号
 *
 * @author ling
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mini/banner")
public class SysMiniBannerController extends BaseController {

    private final ISysMiniBannerService sysMiniBannerService;

    /**
     * 查询小程序Banner列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:list")
    @GetMapping("/list")
    public TableDataInfo<SysMiniBannerVo> list(SysMiniBannerBo bo, PageQuery pageQuery) {
        return sysMiniBannerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序banner列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:export")
    @Log(title = "小程序诊所管理员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMiniBannerBo bo, HttpServletResponse response) {
        List<SysMiniBannerVo> list = sysMiniBannerService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序banner", SysMiniBannerVo.class, response);
    }

    /**
     * 获取小程序banner详细信息
     *
     * @param id 主键
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:query")
    @GetMapping("/{id}")
    public R<SysMiniBannerVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(sysMiniBannerService.queryById(id));
    }

    /**
     * 新增小程序banner详细信息
     */
    @ApiEncrypt
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:add")
    @Log(title = "小程序banner", businessType = BusinessType.INSERT)
    @Lock4j
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMiniBannerBo bo) {

        return toAjax(TenantHelper.ignore(() -> sysMiniBannerService.insertByBo(bo)));
    }

    /**
     * 修改小程序banner详细信息
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:edit")
    @Log(title = "小程序banner", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMiniBannerBo bo) {

        return toAjax(sysMiniBannerService.updateByBo(bo));
    }



    /**
     * 删除诊所小程序管理员
     *
     * @param ids 主键串
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:mini:banner:remove")
    @Log(title = "小程序banner", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysMiniBannerService.deleteWithValidByIds(List.of(ids)));
    }

}
